AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

AddCSLuaFile("cl_buildmode.lua")

AddCSLuaFile("sh_loot.lua")
AddCSLuaFile("sh_turrets.lua")
AddCSLuaFile("sh_items.lua")
AddCSLuaFile("sh_constants.lua")

-- vgui files
AddCSLuaFile("vgui/custom/build.lua")
AddCSLuaFile("vgui/custom/button.lua")
AddCSLuaFile("vgui/custom/closeButton.lua")
AddCSLuaFile("vgui/custom/inventory.lua")
AddCSLuaFile("vgui/custom/menu.lua")
AddCSLuaFile("vgui/custom/spawnicon.lua")
AddCSLuaFile("vgui/custom/infobox.lua")

AddCSLuaFile("vgui/custom/menu_plugins/menu_stats.lua")
AddCSLuaFile("vgui/custom/menu_plugins/menu_upgrade.lua")


-- adding resources
-- icons
resource.AddFile("materials/gui/nonsense/close.vmt")
resource.AddFile("materials/gui/nonsense/close2.vmt")
resource.AddFile("materials/gui/nonsense/info.vmt")
resource.AddFile("materials/gui/nonsense/upgrade.vmt")
resource.AddFile("materials/gui/nonsense/icons/icon_GunTurret1.vmt")
resource.AddFile("materials/gui/nonsense/icons/icon_GunTurret2.vmt")
resource.AddFile("materials/gui/nonsense/icons/icon_GunTurret3.vmt")
resource.AddFile("materials/gui/nonsense/icons/icon_GunTurret4.vmt")
resource.AddFile("materials/gui/nonsense/icons/icon_FireTurret1.vmt")
resource.AddFile("materials/gui/nonsense/icons/icon_FireTurret2.vmt")
resource.AddFile("materials/gui/nonsense/icons/icon_FireTurret3.vmt")
resource.AddFile("materials/gui/nonsense/icons/icon_FireTurret4.vmt")
-- fonts
resource.AddFile("resource/fonts/BIRTH_OF_A_HERO.ttf")
resource.AddFile("resource/fonts/Harabara.ttf")
resource.AddFile("resource/fonts/Paskowy.ttf")
resource.AddFile("resource/fonts/VeryDamaged.ttf")
-- models
--resource.AddFile("models/turrets/sentry1.mdl")


include("shared.lua")


function ToggleBuildMode(pl)
	umsg.Start("ToggleBuildMode", pl)
		umsg.Bool(pl.BuildMode)
	umsg.End()
end

function GM:ShowTeam(pl)
	-- Enable Build Mode
	pl.BuildMode = not pl.BuildMode	
	pl:SetEyeAngles(Angle(45,0,0))	
	ToggleBuildMode(pl)
end

function GM:ShowSpare1(pl)
	local turret = ents.Create("adv_turret_gun")
	turret:SetPos(pl:GetPos() + Vector(256,0,0))
	turret:SetAngles(pl:GetAngles())
	turret:Spawn()	
	turret:Owner(pl)
	local phys = turret:GetPhysicsObject()	
	if phys then
		phys:EnableMotion(false)
	end
end


function GM:PlayerInitialSpawn(pl) 
	pl.BuildMode = false
end

function GM:EntityTakeDamage( ent, inflictor, attacker, amount ) 
	print(amount) 
end


function HookPlDeath(pl, weapon, killer)
	pl.BuildMode = false
	ToggleBuildMode(pl)
end
hook.Add("PlayerDeath", "PlayerDeathHook", HookPlDeath)

function ScaleDamage( ply, hitgroup, dmginfo ) 
		dmginfo:ScaleDamage( 0.2 ) 
end
hook.Add("ScalePlayerDamage","ScaleDamage",ScaleDamage)



--[[ Console commands ]]--

concommand.Add("upgradeTurret", function(pl, com, args)
	if not (pl:IsPlayer() and args) then return end
	if #args<1 then return end
	
	local turretID = args[1]
	local myTurret = Entity(turretID)
	
	if myTurret.ID then
		myTurret:Upgrade()
	end
end)

concommand.Add("showTurretInfo", function(pl, com, args)
	if not args or not pl:IsPlayer() then return end
	local tID = tonumber(args[1])
		Entity(tID):OpenMenu()
end)


concommand.Add("buildTurret", function(pl, com, args)
	if not args or not pl:IsPlayer() or not pl.BuildMode then return end
	if #args!=7 then return end
	
	local turretID = args[1]
	local position = Vector(tonumber(args[2]), tonumber(args[3]), tonumber(args[4]))
	local angles = Angle(tonumber(args[5]), tonumber(args[6]), tonumber(args[7]))	
	
		local turret = ents.Create(turretID)
		turret:SetPos(position)
		turret:SetAngles(angles)
		turret:Spawn()	
		turret:Owner(pl)
		local phys = turret:GetPhysicsObject()	
		if phys and phys:IsValid() then
			phys:EnableMotion(false)
		end
end)

concommand.Add("openInventory", function(pl, com, args)
	if not pl:IsPlayer() then return end
	
	local ent = pl
	
	if #args == 1 then
		for _, ply in pairs(player.GetAll()) do
			if ply:Name() == args[1] then
				ent = ply
				break
			end
		end
	elseif #args>1 then return end
	
	umsg.Start("OpenInventory", pl)
		umsg.Entity(ent)
	umsg.End()
end)
		